package com.zzx.graduation.controller;



import com.zzx.graduation.config.JwtConfig;
import com.zzx.graduation.entity.User;
import com.zzx.graduation.response.ResponseMessage;
import com.zzx.graduation.response.StateCode;
import com.zzx.graduation.service.UserCRUD;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.Transactional;
import javax.validation.constraints.Null;
import java.util.List;


//@Transactional
@RestController
@CrossOrigin
@Transactional
@RequestMapping("/api/user")
public class UserManagerController {

    //slf4j.Logger
    private  static final Logger log = LoggerFactory.getLogger(UserManagerController.class);


    @Autowired
    UserCRUD userService;//Service

    /**
     * select 查询操作
     * 具有幂等性
     * @return
     */
    @GetMapping("/users")
    @ResponseStatus(HttpStatus.OK)
    public ResponseMessage getUsers(){
        List<User> users = userService.findUsers();
        if(users == null)
        {
            return  new ResponseMessage<List<User>>(StateCode.NOT_FOUNT,"查询失败",users);
        }

        return   new ResponseMessage<List<User>>(StateCode.SUCCESS,"查询成功",users);
    }


    /**
     * SELECT 查询操作，
     * 具有幂等性
     * */
    @GetMapping("/user/{id}")
    @ResponseStatus(HttpStatus.OK)
    public ResponseMessage getUser(@PathVariable("id") Integer id){

        if(null == id){
            return  null;
        }
        User users = userService.findById(id);
        if(users == null)
        {
            return  new ResponseMessage<User>(StateCode.NOT_FOUNT,"查询失败",users);
        }

        return   new ResponseMessage<User>(StateCode.SUCCESS,"查询成功",users);
    }

    /**
     * 新增一个用户对象
     * 非幂等
     * */
    @PostMapping("/user")
    @ResponseStatus(HttpStatus.CREATED)
    public ResponseMessage addUser(@RequestBody User user){
        if(null == user){
            return  null;
        }
        User users = userService.addUser(user);
        if(users == null)
        {
            return  new ResponseMessage<User>(StateCode.NOT_FOUNT,"新增失败",users);
        }

        return   new ResponseMessage<User>(StateCode.SUCCESS,"新增成功",users);
    }


    /**
     * 编辑一个用户对象
     * 幂等性
     * */
    @PutMapping("/user/{id}")
    @ResponseStatus(HttpStatus.CREATED)
    public ResponseMessage editUser(@PathVariable("id") Integer id,@RequestBody User user){
        if(null == user){
            return  null;
        }
        user.setId(id);
        User users = userService.modifyUser(user);
        if(users == null)
        {
            return  new ResponseMessage<User>(StateCode.NOT_FOUNT,"修改失败",users);
        }

        return   new ResponseMessage<User>(StateCode.SUCCESS,"修改成功",users);
    }

    /**
     * 删除一个用户对象
     * 幂等性
     * */
    @DeleteMapping("/user/{id}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public ResponseMessage deleteUser(@PathVariable("id") Integer id){
        if(null == id){
            return  null;
        }
        Boolean b = userService.deleteUserById(id);
        if(b == null || b == false)
        {
            return  new ResponseMessage<User>(StateCode.NOT_FOUNT,"删除失败",null);
        }

        return   new ResponseMessage<User>(StateCode.SUCCESS,"删除成功",null);
    }


}
